<?php

/**
 * @author      :   QuangLT
 * @name        :   Thethao_Business_Block
 * @copyright   :   Fpt Online
 * @todo        :   Block business
 */
class Fpt_Business_Category_Adapter_MySql extends Fpt_Business_Category_Adapter_Abstract
{

    protected static $_instance = null;

    /**
     * Constructor of class
     * we don't permit an explicit call of the constructor! (like $v = new Singleton())
     */
    public function __construct()
    {
        
    }

    /**
     * Get singletom instance
     * @return <object>
     */
    public final static function getInstance()
    {
        //Check instance
        if (is_null(self::$_instance))
        {
            self::$_instance = new self();
        }

        //Return instance
        return self::$_instance;
    }

    /**
     * Get category by parent id
     * @param int $intCategoryID
     * @return <array> Article information 
     */
    public function getCategoryByParentID($intCategoryID)
    {
        try
        {
            $sport_m = Fpt_Global::getSportDB();
            // Get user activeation key
            $objStmt = $sport_m->prepare('CALL sp_getCategory(:p_article)');

            // Bind param
            $objStmt->bindParam('p_article', $intCategoryID, PDO::PARAM_INT);

            // Fetch result
            $objStmt->execute();
            $arrArticleInfo = $objStmt->fetchAll();

            // Close cursor
            $objStmt->closeCursor();

            // Release
            unset($objStmt);
        } 
        catch (Zend_Db_Exception $ex)
        {
            //log error
            Fpt_Log::sendlog(4,$ex->getCode(),$ex->getMessage(),$ex->getTraceAsString(),null, null);
            return false;
        }
        return $arrArticleInfo;
    }

    /**
     * Get category by category code
     * @param string $strCode
     * @return <array> category information 
     * @author: phongtx@fpt.net
     */
    public function getCategoryByCode($strCode)
    {
        try
        {
            //get master db instance
            $sport_m = Fpt_Global::getSportDB();
            //call sp get detail
            $stmt = $sport_m->prepare('CALL sp_getCategoryByCatecode(:p_catecode)');

            //Bind param
            $stmt->bindParam('p_catecode', $strCode, PDO::PARAM_STR);

            //Fetch result
            $stmt->execute();
            
            $arrResult = $stmt->fetch();

            //Close cursor
            $stmt->closeCursor();

            //Release
            unset($stmt);
        } 
        catch (Zend_Exception $ex)
        {
            //log error
            Fpt_Log::sendlog(4,$ex->getCode(),$ex->getMessage(),$ex->getTraceAsString(),null, null);
            return false;
        }
        return $arrResult;
    }
    
     /**
     * Get category by category category id
     * @param string $strCode
     * @return <array> category information 
     * @author: nhanva@fpt.net
     */
    public function getCategoryByID($intCategoryID)
    {
        try
        {
            //get master db instance
            $sport_m = Fpt_Global::getSportDB();
            //call sp get detail
            $stmt = $sport_m->prepare('CALL sp_getCategoryByID(:p_categoryid)');

            //Bind param
            $stmt->bindParam('p_categoryid', $intCategoryID, PDO::PARAM_STR);

            //Fetch result
            $stmt->execute();
            
            $arrResult = $stmt->fetch();

            //Close cursor
            $stmt->closeCursor();

            //Release
            unset($stmt);
        } 
        catch (Zend_Exception $ex)
        {
            //log error
            Fpt_Log::sendlog(4,$ex->getCode(),$ex->getMessage(),$ex->getTraceAsString(),null, null);
            return false;
        }
        return $arrResult;
    }

    /**
     * Get category by CateType
     * @param string $strCode
     * @return <array> category information
     * @author: kietthm@fpt.net
     */
    public function getCategoryByCateType($intCateType)
    {
        try
        {
            //get master db instance
            $sport_m = Fpt_Global::getSportDB();
            //call sp get detail
            $stmt = $sport_m->prepare('CALL sp_getCategoryByCateType(:p_catetype)');

            //Bind param
            $stmt->bindParam('p_catetype', $intCateType, PDO::PARAM_INT);

            //Fetch result
            $stmt->execute();

            $arrResult = $stmt->fetchAll();

            //Close cursor
            $stmt->closeCursor();

            //Release
            unset($stmt);
        }
        catch (Zend_Exception $ex)
        {
            //log error
            Fpt_Log::sendlog(4,$ex->getCode(),$ex->getMessage(),$ex->getTraceAsString(),null, null);
            return false;
        }
        return $arrResult;
    }
    
     /**
     * Get full category include all sub category
     * @param array $intCategoryID
     * @return array
     * @author nhanva@fpt.net
     */
    public function getFullCategoryByParentID($intCategoryID)
    {
        try
        {
            $sport_m = Fpt_Global::getSportDB(); 
            // Get user activeation key
            $objStmt = $sport_m->prepare('CALL sp_getCategoryFullByParentID(:p_parentid)');

            // Bind param
            $objStmt->bindParam('p_parentid', $intCategoryID, PDO::PARAM_INT);

            // Fetch result
            $objStmt->execute();
            $arrArticleInfo = $objStmt->fetchAll();

            // Close cursor
            $objStmt->closeCursor();

            // Release
            unset($objStmt);
        }
        catch (Zend_Db_Exception $ex)
        {
           Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
        }
        return $arrArticleInfo;
    }

}

